<?php
header('Content-Type: text/html; charset=utf-8');
header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' );
header ("Last-Modified: " . gmdate ("D, d M Y H:i:s") . " GMT");
header( 'Cache-Control: no-store, no-cache, must-revalidate' );
header( 'Cache-Control: post-check=0, pre-check=0', false );
header( 'Pragma: no-cache' );

include_once("chklogin.php");

$result_data="<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\">";
$result_data.="<tr>";
$result_data.="<td class=\"main_menu_tab\" height=\"30\">&nbsp;&nbsp;<b>Manage&nbsp;Product</b></td>";
$result_data.="</tr>";
$result_data.="<tr><td height=\"10\"></td></tr>";

$result_data.="<tr><td>";
$result_data.="<span id=\"menu_product\">";
$result_data.="<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"table_menu\">";
$result_data.="<tr class=\"font_normal\">";
$result_data.="<td class=\"menu_tab\" height=\"20\">&nbsp;&nbsp;<b>Menu&nbsp;[Create, Edit and Delete]</b></td>";
$result_data.="</tr>";
$result_data.="<tr><td height=\"10\"></td></tr>";

$sql="select * from product order by product_id DESC";
mysql_db_query($dbm,"SET NAMES utf8");
$result=mysql_db_query($dbm, $sql);

if(mysql_num_rows($result) > 0){
	$product_id=mysql_result($result, 'product_id');
	$var_prd=str_replace("PID-", "", $product_id);
	$num=strlen($var_prd);

	for($i=0;$i<$num;$i++){
		if((int)$var_prd[$i] == 0){
			$var_replace.=$var_prd[$i];
			continue;
		}break;
	}
	$var_prd=str_replace($var_replace, "", $var_prd);
	
	$len_str=strlen((int)$var_prd+1);
	$len_str=8-$len_str;
	
	$product_no="PID-";
		for($i=0;$i<8;$i++){
			if($i !=$len_str) $product_no.="0";
			else{
			$product_no.=(int)$var_prd+1;
			break;
		}
	}
}else{
	$product_no="PID-00000001";
}
mysql_free_result($result);

$result_data.="<tr><td>";
$result_data.="<div style=\"overflow: auto; width:800px; height: 150px;\">";
$result_data.="<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
  $result_data.="<tr>";
    $result_data.="<td align=\"right\" class=\"font_normal\"><b>Product&nbsp;ID&nbsp;:&nbsp;</b></td>";
    $result_data.="<td align=\"left\"><input type=\"text\" name=\"product_id\" id=\"product_id\" value='".$product_no."' readonly></td>";
    $result_data.="<td width=\"30\">&nbsp;</td>";
    $result_data.="<td align=\"right\" class=\"font_normal\"><b>Ownership&nbsp;:&nbsp;</b></td>";
    $result_data.="<td align=\"left\"><input type=\"text\" name=\"ownership\" id=\"ownership\">&nbsp;<font color=\"#FF0000\">*</font></td>";
 $result_data.="</tr>";
  $result_data.="<tr><td colspan=\"5\" height=\"10\"></td></tr>";

  $result_data.="<tr>";
    $result_data.="<td align=\"right\" class=\"font_normal\"><b>Product&nbsp;Name&nbsp;[thai]&nbsp;:&nbsp;</b></td>";
    $result_data.="<td align=\"left\"><input type=\"text\" name=\"product_name_th\" id=\"product_name_th\">&nbsp;<font color=\"#FF0000\">*</font></td>";
    $result_data.="<td width=\"30\">&nbsp;</td>";
    $result_data.="<td align=\"right\" class=\"font_normal\"><b>Product&nbsp;Name&nbsp;[eng]&nbsp;:&nbsp;</b></td>";
    $result_data.="<td align=\"left\"><input type=\"text\" name=\"product_name_en\" id=\"product_name_en\"></td>";
  $result_data.="</tr>";
  $result_data.="<tr>";
    $result_data.="<td colspan=\"5\" height=\"10\"></td>";
  $result_data.="</tr>";

  $result_data.="<tr>";
     $result_data.="<td align=\"right\" class=\"font_normal\" valign=\"top\"><b>Property&nbsp;:&nbsp;</b></td>";
     $result_data.="<td colspan=\"4\" align=\"left\">";
       $result_data.="<textarea name=\"property\" id=\"property\" cols=\"60\" rows=\"3\"></textarea>&nbsp;<font color=\"#FF0000\">*</font>";
    $result_data.="</td>";
  $result_data.="</tr>";
   $result_data.="<tr>";
    $result_data.="<td colspan=\"5\" height=\"10\"></td>";
  $result_data.="</tr>";

  $result_data.="<tr>";
     $result_data.="<td align=\"right\" class=\"font_normal\" valign=\"top\"><b>ส่วนผสมของผลิตภัณฑ์&nbsp;<br>(Ingredients)&nbsp;:&nbsp;</b></td>";
     $result_data.="<td colspan=\"4\" align=\"left\">";
       $result_data.="<textarea name=\"ingredients\" id=\"ingredients\" cols=\"60\" rows=\"3\"></textarea>&nbsp;<font color=\"#FF0000\">*</font>";
    $result_data.="</td>";
  $result_data.="</tr>";
   $result_data.="<tr>";
    $result_data.="<td colspan=\"5\" height=\"10\"></td>";
  $result_data.="</tr>";

$result_data.="<tr>";
    $result_data.="<td align=\"right\" class=\"font_normal\"><b>Net&nbsp;Weight&nbsp;:&nbsp;</b></td>";
    $result_data.="<td align=\"left\"><input type=\"text\" name=\"net_weight\" id=\"net_weight\">&nbsp;<font color=\"#FF0000\">*</font></td>";
    $result_data.="<td width=\"30\">&nbsp;</td>";
    $result_data.="<td align=\"right\" class=\"font_normal\"><b>Price&nbsp;:&nbsp;</b></td>";
    $result_data.="<td align=\"left\"><input type=\"text\" name=\"product_price\" id=\"product_price\">&nbsp;<font color=\"#FF0000\">*</font></td>";
  $result_data.="</tr>";
  $result_data.="<tr>";
    $result_data.="<td colspan=\"5\" height=\"10\"></td>";
  $result_data.="</tr>";

$result_data.="<tr>";
     $result_data.="<td align=\"right\" class=\"font_normal\" valign=\"top\"><b>วิธีใช้&nbsp;(Instruction)&nbsp;:&nbsp;</b></td>";
     $result_data.="<td colspan=\"4\" align=\"left\">";
       $result_data.="<textarea name=\"instruction\" id=\"instruction\" cols=\"60\" rows=\"3\"></textarea>&nbsp;<font color=\"#FF0000\">*</font>";
    $result_data.="</td>";
  $result_data.="</tr>";
   $result_data.="<tr>";
    $result_data.="<td colspan=\"5\" height=\"10\"></td>";
  $result_data.="</tr>";

  $result_data.="<tr>";
     $result_data.="<td align=\"right\" class=\"font_normal\" valign=\"top\"><b>รายละเอียดเพิ่มเติม&nbsp;<br>(Remark)&nbsp;:&nbsp;</b></td>";
     $result_data.="<td colspan=\"4\" align=\"left\">";
       $result_data.="<textarea name=\"remark\" id=\"remark\" cols=\"60\" rows=\"3\"></textarea>";
    $result_data.="</td>";
  $result_data.="</tr>";
   $result_data.="<tr>";
    $result_data.="<td colspan=\"5\" height=\"10\"></td>";
  $result_data.="</tr>";

  $result_data.="<tr>";
    $result_data.="<td align=\"right\" class=\"font_normal\"><b>Status&nbsp;Import&nbsp;:&nbsp;</b></td>";
    $result_data.="<td align=\"left\">";
		$result_data.="<select id=\"status_import\">";
			$result_data.="<option value=\"0\">InActive</option>";
			$result_data.="<option value=\"1\">Active</option>";
		$result_data.="</select>";
	$result_data.="</td>";
    $result_data.="<td width=\"30\">&nbsp;</td>";
    $result_data.="<td align=\"right\" class=\"font_normal\"><b>Sub_Product&nbsp;Type&nbsp;:&nbsp;</b></td>";
    $result_data.="<td align=\"left\">";
		$result_data.="<select id=\"sub_group_id\">";
				$sql="select * from product_subgroup order by subgroup_id";
				mysql_db_query($dbm,"SET NAMES utf8");
				$result=mysql_db_query($dbm, $sql);
				while($record=mysql_fetch_array($result, MYSQL_ASSOC)){
					$result_data.="<option value='".$record["subgroup_id"]."'>".$record["subgroup_desc"]."</option>";
				}
				mysql_free_result($result);
		$result_data.="</select>";
	$result_data.="</td>";
  $result_data.="</tr>";
	$result_data.="<tr>";
    $result_data.="<td colspan=\"5\" height=\"10\"></td>";
  $result_data.="</tr>";

	$result_data.="<tr class=\"font_normal\">";
     $result_data.="<td align=\"right\" valign=\"top\"><b>Upload&nbsp;Photo&nbsp;:&nbsp;</b></td>";
     $result_data.="<td colspan=\"4\" align=\"left\">";
       $result_data.="<iframe id=\"up_file\" name=\"up_file\" style=\"width:0px;height:0px;border:0\"></iframe>";
	   $result_data.="<form id=\"frmUpload\" action=\"upload_image.php\" method=\"post\" enctype=\"multipart/form-data\" onsubmit=\"return clickupload();\" target=\"up_file\">";
		$result_data.="<input type=\"hidden\" id=\"photo_history\" name=\"photo_history\" value=\"\">";
		$result_data.="<input id=\"fileupload\" name=\"fileupload\" type=\"file\">&nbsp;";
			$result_data.="<input id=\"btnUpload\" type=\"submit\" value=\"Attachment\">";
	   $result_data.="</form>";
		$result_data.="<span id=\"upmsg\"></span>";
    $result_data.="</td>";
  $result_data.="</tr>";

  $result_data.="</table>";
  $result_data.="</div>";
 
  $result_data.="</td>";
  $result_data.="</tr>";
   $result_data.="<tr><td height=\"10\"></td></tr>";
	$result_data.="<tr>";
		$result_data.="<td align=\"left\">";

		if(ereg("MSIE", $_SERVER["HTTP_USER_AGENT"])){
			$bt_style="background-image:url(../image/add_button.png)no-repeat;width:70px;height:25px;outline:none;";
		}

		$result_data.="<button id=\"btn_add\" onclick=\"fn_add('product');\" style=\"".$bt_style."\">Add</button>&nbsp;";
		$result_data.="<button id=\"btn_cancel\" onclick=\"fn_cancel('product');\" style=\"".$bt_style."\">Cancel</button>";
		//$result_data.="<button onclick=\"fn_add('product')\" style=\"background-image:url(../image/add_button.png); width:77px;height:30px;\">Add</button>&nbsp;";
		//$result_data.="<input type=\"button\" value=\"Cancel\" onclick=\"fn_cancel('product');\">";
		$result_data.="</td>";
	$result_data.="</tr>";
  $result_data.="</table>";
  $result_data.="</span>";
$result_data.="</td>";
	$result_data.="</tr>";

$result_data.="<tr><td height=\"10\"></td></tr>";

//----------- Show Data --------
$result_data.="<tr>";
	$result_data.="<td>";
		$result_data.="<table cellspacing=\"0\" cellpadding=\"0\" class=\"table_menu\" width=\"100%\">";
			$result_data.="<tr class=\"font_normal\">";
			$result_data.="<td class=\"menu_tab_b\" height=\"30\"><font color=\"#2A3FFF\">&nbsp;&nbsp;<b>List&nbsp;Product</b></font></td>";
			$result_data.="</tr>";
			$result_data.="<tr><td>";
			$result_data.="<span id=\"data_product\">";
			$result_data.="<div id=\"div_data_product\" style=\"overflow:auto;\">";
		
				$sql="select * from product order by product_id ASC";
				//mysql_db_query($dbm,"SET NAMES utf8");
				//mysql_db_query($dbm,"SET character_set_results=tis620");
				$result=mysql_db_query($dbm, $sql);
				
				
				/*if(ereg("MSIE", $_SERVER["HTTP_USER_AGENT"])){
					
					//$tr_join_top="position:relative; top:expression(this.offsetParent.scrollTop);height:25px;";
				}*/
				
				$result_data.="<table id=\"tb_data_product\" cellspacing=\"1\" cellpadding=\"0\" width=\"100%\">";
				
				$n=0;
				
				while($record=mysql_fetch_array($result, MYSQL_ASSOC)){
					$class_td=(($n % 2) == 0) ? 1 : 2;
					$result_data.="<tr><td class=\"td_data_".$class_td."\">";
					$result_data.="<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"100%\">";
					$result_data.="<tr><td>";

					$result_data.="<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\">";

					$result_data.="<tr class=\"font_normal\">";
						$result_data.="<td width=\"80\">";
							$result_data.="<a id=\"sh_tr_product_".$n."\" href=\"javascript:void(0);\" onclick=\"show_tr('tr_product_".$n."');\">";
							$result_data.="<img src=\"../image/collapse.gif\" border=\"0\">";
							$result_data.="</a>";

							$result_data.="&nbsp;";
							$result_data.="<a href=\"javascript:void(0);\" onclick=\"fn_edit('product', '".$record["product_id"]."');\">";
							$result_data.="<img src=\"../image/edit.gif\" border=\"0\" style=\"background-color:transparent;\">";
							$result_data.="</a>";

							$result_data.="&nbsp;";
							$result_data.="<a href=\"javascript:void(0);\" onclick=\"fn_delete('product', '".$record["product_id"]."');\">";
							$result_data.="<img src=\"../image/delete.gif\" border=\"0\" style=\"background-color:transparent;\">";
							$result_data.="</a>";

							$result_data.="</td>";
						$result_data.="<td align=\"right\"><b>Product&nbsp;ID&nbsp;:&nbsp;</b></td>";
						$result_data.="<td align=\"left\">".$record["product_id"]."</td>";
						$result_data.="<td width=\"100\">&nbsp;</td>";
						$result_data.="<td align=\"right\"><b>OwnerShip&nbsp;:&nbsp;</b></td>";
						$result_data.="<td align=\"left\">".$record["ownership"]."</td>";
					$result_data.="</tr>";
					$result_data.="<tr><td height=\"5\" colspan=\"6\"></td></tr>";

					$result_data.="<tr class=\"font_normal\">";
						$result_data.="<td width=\"80\">&nbsp;</td>";
						$result_data.="<td align=\"right\"><b>Product&nbsp;Name&nbsp;[thai]&nbsp;:&nbsp;</b></td>";
						$result_data.="<td align=\"left\">".$record["product_tname"]."</td>";
						$result_data.="<td width=\"100\">&nbsp;</td>";
						$result_data.="<td align=\"right\"><b>Product&nbsp;Name&nbsp;[eng]&nbsp;:&nbsp;</b></td>";
						$result_data.="<td align=\"left\">".$record["product_ename"]."</td>";
					$result_data.="</tr>";
					$result_data.="<tr><td height=\"5\" colspan=\"6\"></td></tr>";

					$result_data.="<tr class=\"font_normal\">";
						$result_data.="<td width=\"80\">&nbsp;</td>";
						$result_data.="<td align=\"right\"><b>น้ำหนักผลิตภัณฑ์&nbsp;:&nbsp;</b></td>";
						$result_data.="<td align=\"left\">".$record["net_weight"]."</td>";
						$result_data.="<td width=\"100\">&nbsp;</td>";
						$result_data.="<td align=\"right\"><b>ราคาผลิตภัณฑ์&nbsp;:&nbsp;</b></td>";
						$result_data.="<td align=\"left\">".(float)$record["product_price"]."</td>";
					$result_data.="</tr>";
					$result_data.="<tr><td height=\"10\" colspan=\"6\"></td></tr>";
					$result_data.="</table>";
					$result_data.="</td></tr>";

					$result_data.="<tr id=\"tr_product_".$n."\" style=\"display:none;\">";
						$result_data.="<td>";
						$result_data.="<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\">";
							$result_data.="<tr class=\"font_normal\">";
							$result_data.="<td align=\"right\" valign=\"top\"><b>คุณสมบัติ&nbsp;:&nbsp;</b></td>";
							$result_data.="<td align=\"left\">".$record["property"]."</td>";
							$result_data.="</tr>";
							$result_data.="<tr><td height=\"5\" colspan=\"2\"></td></tr>";
							
							$result_data.="<tr class=\"font_normal\">";
							$result_data.="<td align=\"right\" valign=\"top\"><b>ส่วนผสม&nbsp;:&nbsp;</b></td>";
							$result_data.="<td align=\"left\">".$record["ingredients"]."</td>";
							$result_data.="</tr>";
							$result_data.="<tr><td height=\"5\" colspan=\"2\"></td></tr>";

							$result_data.="<tr class=\"font_normal\">";
							$result_data.="<td align=\"right\" valign=\"top\"><b>วิธีการใช้&nbsp;:&nbsp;</b></td>";
							$result_data.="<td align=\"left\">".$record["instruction"]."</td>";
							$result_data.="</tr>";
							$result_data.="<tr><td height=\"5\" colspan=\"2\"></td></tr>";

						$result_data.="</table>";
						$result_data.="</td>";
					$result_data.="</tr>";
					$result_data.="</table>";
					$result_data.="</td></tr>";
					$n++;
				}
				mysql_free_result($result);
				$result_data.="</table>";

			$result_data.="</div>";
			$result_data.="</span>";
			$result_data.="</td>";
			$result_data.="</tr>";
		$result_data.="</table>";		
	$result_data.="</td>";
$result_data.="</tr>";

$result_data.="</table>";

mysql_close($connect);

$json[] = array(	'result_data'			=> $result_data
						);
ob_start ("ob_gzhandler");
header( 'Content-Type: text/javascript; charset:UTF-8' );
header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' );
header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:S' ) . ' GMT' );
header( 'Cache-Control: no-store, no-cache, must-revalidate' );
header( 'Cache-Control: post-check=0, pre-check=0', false );
header( 'Pragma: no-cache' );
print(json_encode($json));
?>